/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import com.eelpo.common.util.BlankUtils;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * <pre>
 * 
 * 权限实体类，用于映射T_PERMISSION表(Persist Object)
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Entity
@Table(name = "T_PERMISSION", uniqueConstraints = { @UniqueConstraint(columnNames = { "entityId", "actionId" }) })
public class TPermission implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -1442402127272949704L;
	/**
	 * 权限Id
	 */
	@NotEmpty
	private String id;

	/**
	 * 权限代码
	 */
	@NotEmpty
	private String code;

	/**
	 * 权限状态
	 */
	@NotNull
	private Byte status;

	/**
	 * 权限操作的实体
	 */
	@NotNull
	private TEntity entity;

	/**
	 * 权限所做的操作
	 */
	@NotNull
	private TAction action;

	/**
	 * 拥有该权限的角色
	 */
	private Set<TRole> roles = new HashSet<TRole>();

	public TPermission() {

	}

	public TPermission(String id) {
		this.id = id;
	}

	@Id
	@Column(length = 36)
	@GenericGenerator(name = "uuid", strategy = "uuid2")
	@GeneratedValue(generator = "uuid")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(nullable = false, length = 128)
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Column(nullable = false)
	public Byte getStatus() {
		return status;
	}

	public void setStatus(Byte status) {
		this.status = status;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "entityId", nullable = false)
	public TEntity getEntity() {
		return entity;
	}

	public void setEntity(TEntity entity) {
		this.entity = entity;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "actionId", nullable = false)
	public TAction getAction() {
		return action;
	}

	public void setAction(TAction action) {
		this.action = action;
	}

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "permissions")
	public Set<TRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<TRole> roles) {
		this.roles = roles;
	}

	@Override
	public boolean equals(Object obj) {
		if (BlankUtils.isBlank(obj) || !(obj instanceof TPermission))
			return false;

		if (this == obj)
			return true;

		TPermission permission = (TPermission) obj;

		if (BlankUtils.isBlank(this.id) || BlankUtils.isBlank(permission.getId())) {
			return false;
		}

		return this.id.equals(permission.getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public String toString() {
		return "TPermission [code=" + code + "]";
	}
}
